import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class PlayingSide
{
  private Player [] players;
  private PlayingPosition [] positions;
  private HashMap <Player,PlayingPosition> player_position_map;

  private ArrayList <Player> subs;

  public PlayingSide ()
  {
    players = new Player [11];
    player_position_map = new HashMap <Player, PlayingPosition> ();
  }

  public void set_formation (PlayingPosition [] positions)
  {
    player_position_map.clear();
    this.positions = positions;
  }

  public void set_position (Player player, PlayingPosition position) throws PlayerSelectionException
  {
    check_position(position);
    player_position_map.put(player,position);
  }

  private void check_position (PlayingPosition position) throws PlayerSelectionException
  {
    boolean found = false;
    for (int i=0; i < positions.length; i++){
      if (position.equals(positions[i])){
        found = true;
        break;
      }
    }

    if (!found){
      throw new PlayerSelectionException("Error position is not in formation");
    }    
  }

  public Iterator <Player> iterator ()
  {
    return player_position_map.keySet().iterator();
  }

  public PlayingPosition get_position (Player player)
  {
    return player_position_map.get(player);
  }
}
